import requests
import json
import os

def get_tasks():

    with open("./ignore/config.json","r") as f:
        config=json.load(f)

    token = config["DIDA_token"]

    project_id = config["DIDA_project_ID"]
    url = f"https://api.dida365.com/open/v1/project/{project_id}/data"


    headers = {
        "Authorization": f"Bearer {token}",
        "User-Agent": "Mozilla/5.0"  # 模拟浏览器请求
    }

    try:
        response = requests.get(url, headers=headers)
        
        # 输出原始响应（调试用）
        # print("原始响应:", response.text)
        
        if response.status_code == 200:
            # print("请求成功！")
            res = response.json()
            tasks = []
            for i in res["tasks"]:
                tasks.append([i["sortOrder"],i["title"]])
                if "dueDate" in i:
                    tasks[-1].append(i["dueDate"])

            tasks.sort(key=lambda x:x[0])
            # print(tasks)
            return tasks
        else:
            print(f"请求失败，状态码: {response.status_code}")
            print("错误信息:", response.text)

    except requests.exceptions.RequestException as e:
        print("请求发生错误:", e)

    return []

if __name__ == "__main__":
    get_tasks()